package JB1;
import java.util.Scanner;
public class Anagram {

	public static void main(String[] args) {
		Scanner sc =  new Scanner (System.in);
		System.out.print("Please enter 1st word/phrase:");
		String first = sc.nextLine();
		System.out.print("Please enter 2nd word/phrase:");
		String second = sc.nextLine();
		if (isAnagram(first,second))System.out.println("It is Anagram");
		else System.out.println("It is NOT Anagram");
	}
	static String getSortStr (String in){

		char[] charIn = in.toCharArray();

		for (int i = 0; i < (charIn.length - 1); i++) {
			for (int j = i + 1; j > 0; j--) {
				if (charIn[j] < charIn[j - 1]) {
					char temp = charIn[j - 1];
					charIn[j - 1] = charIn[j];
					charIn[j] = temp;
				}
			}
		}
		String sortStr = String.valueOf(charIn);
		return sortStr;
	}
	static boolean isAnagram (String frst,String scnd){
		if (frst.length() != scnd.length()){
			return false;
		}
		String sortFrst = getSortStr(frst);
		String sortScnd = getSortStr(scnd);
		for (int i=0;i< sortFrst.length();i++){
		if (sortFrst.charAt(i)!=sortScnd.charAt(i)){
			return false;
		}
		}
		return true;
	}
}
